home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 8598 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.6 KB  |  68 lines

  1. Newsgroups: comp.lang.c++,comp.os.ms-windows.programmer.misc,owl.development
  2. Path: netcom.com!marnold
  3. From: marnold@netcom.com (Matt Arnold)
  4. Subject: Re: Compiler Bug - can call private constructors.
  5. Message-ID: <marnoldDMyJCH.J7x@netcom.com>
  6. Organization: NETCOM On-line Communication Services (408 261-4700 guest)
  7. References: <311a149f.8353477@hector> <4fnk8n$17e@daffodil.InfoChan.COM>
  8. Date: Sun, 18 Feb 1996 06:00:17 GMT
  9. Sender: marnold@netcom5.netcom.com
  10.  
  11. alanj@infochan.com (Alan Johnston) writes:
  12.  
  13. >Kenn@owl-uk.co.uk (Ken Nicolson) wrote:
  14. >>>>>> SAMPLE CODE REPRODUCING PROBLEM
  15.  
  16. >>#include <stdio.h>
  17. >>#include <iostream.h>
  18.  
  19. >>class FOOBAR
  20. >>{
  21. >>private:
  22. >>    FOOBAR()
  23. >>    {
  24. >>        cout << "I've called a private constructor!\n";
  25. >>    }
  26. >>};
  27.  
  28.  
  29. >>FOOBAR foobar()
  30. >>{
  31. >>//  FOOBAR b; SYNTAX ERROR, as expected
  32. >>    return FOOBAR( );   // This is wrong
  33. >>}
  34.  
  35. >Perhaps the problem stems from the fact that 'return FOOBAR() '
  36. >actually calls the copy constructor.  Since you haven't declared a
  37. >private copy constructor, the compiler is making one for you, and it
  38. >is public.  Try declaring a private copy constructor:
  39. >FOOBAR(const FOOBAR&);
  40.  
  41. That might "fix" things but it doesn't explain the behavior the original 
  42. poster is getting (which is absolutely wrong).
  43.  
  44. Your claim that "return FOOBAR();" causes a call to the copy ctor is true,
  45. but it does not *only* call the copy constructor.  The regular ctor must
  46. be called as well because, obviously, there has to be a FOOBAR to *copy* 
  47. in the first place.  The statement "return FOOBAR();" generates a call to
  48. the *default* ctor, no question, and the above code should not compile 
  49. since the default ctor is declared private. 
  50.  
  51. In fact, the above code does not work under my compiler, Borland C++ 4.52.  
  52. It produces the error that "FOOBAR::FOOBAR() is not accessible", meaning
  53. that it can't be called.  This is absolutely the correct result.  Why 
  54. the latest edition of Visual C++ has a different "opinion" is beyond me. 
  55.  
  56. The orignal poster's code should not compile, period.  If it is "fixed"
  57. by creating a private copy constructor, then that only goes to show that 
  58. something is quite wrong with the compiler he's using.  
  59.  
  60. Regards,
  61. -------------------------------------------------------------------------
  62. Matt Arnold                       |        | ||| | |||| |  | | || ||
  63. marnold@netcom.com                |        | ||| | |||| |  | | || ||
  64. Boston, MA                        |      0 | ||| | |||| |  | | || ||
  65. 617.389.7384 (h) 617.576.2760 (w) |        | ||| | |||| |  | | || ||
  66. C++, MIDI, Win32/95 developer     |        | ||| 4 3 1   0 8 3 || ||
  67. -------------------------------------------------------------------------
  68.